package com.mate.cloud.message.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mate.cloud.cache.service.RedisCacheService;
import com.mate.cloud.common.config.PropertyConfig;
import com.mate.cloud.core.util.Func;
import com.mate.cloud.message.constants.PhoneConstants;
import com.mate.cloud.message.domain.Order;
import com.mate.cloud.message.enums.BaseExceptionCodeEnum;
import com.mate.cloud.message.enums.IMMessageExceptionEnum;
import com.mate.cloud.message.exception.IMMessageException;
import com.mate.cloud.message.mapper.OrderMapper;
import com.mate.cloud.message.service.PhoneService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static com.mate.cloud.message.constants.PhoneConstants.MAX_PHONE_COUNT;

@Service
public class PhoneServiceImpl extends ServiceImpl<OrderMapper, Order> implements PhoneService {

    @Resource
    private RedisCacheService redisCacheService;

    @Override
    public String sendMessage(String phone) {

        String countKey = String.format(phone, PhoneConstants.PHONE_COUNT_SUFFIX);
        String codeKey = String.format(phone, PhoneConstants.PHONE_CODE_SUFFIX);
        String cacheCount = (String) redisCacheService.get(countKey);
        if (redisCacheService.get(codeKey) == null && MAX_PHONE_COUNT.equals(cacheCount)) {
            throw new IMMessageException(BaseExceptionCodeEnum.MAX_PHONE_COUNT.getCode());
        }
        Long exTime  = redisCacheService.getExpire(codeKey);
        if (exTime  > 0) {
            long minute = (exTime / 60);
            long second = (exTime % 60);
            throw new IMMessageException(Integer.valueOf(BaseExceptionCodeEnum.VALIDITY_EXIST_CODE.getCode()), "" + minute + "", "" + second + "");
//            throw new BusinessRuntimeException(BaseExceptionCodeEnum.VALIDITY_EXIST_CODE.getCode(), new String[]{"'" + (exTime / 60) + "'", "'" + (exTime % 60) + "'"});
        }
        String code = getCode();
        redisCacheService.set(codeKey, code, PhoneConstants.MAX_TIME_OUT, TimeUnit.MINUTES);
        redisCacheService.incr(countKey);

        String property = PropertyConfig.getProperty(BaseExceptionCodeEnum.VALIDITY_CODE_TIME.getCode());
        String message = MessageFormat.format(property, code, MAX_PHONE_COUNT);
        return message;
    }

    @Override
    public String check(String phone, String code) {
        String codeKey = String.format(phone, PhoneConstants.PHONE_CODE_SUFFIX);
        if (code != null && code.equals(redisCacheService.get(codeKey))) {
            return "验证成功";
        }
        throw new IMMessageException(BaseExceptionCodeEnum.VALIDITY_CODE_FAIL.getCode());
    }

    private String getCode() {
        return String.valueOf(new Random().nextInt(900000) + 100000);
    }

    @Override
    public void exceptionThrow() {
        throw new IMMessageException(Func.toInt(IMMessageExceptionEnum.IM_REQUEST_ERROR.getCode()));
    }
}
